<template>
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form ref="form" :model="regForm" :rules='rules'>
        <!-- 用户名 -->
         <el-form-item prop='username'>
          <el-input v-model="regForm.username" placeholder="请输入用户名"></el-input>
         </el-form-item>
         <!-- 密码 -->
         <el-form-item prop='password'>
          <el-input type="password" v-model="regForm.password" placeholder="请输入密码"></el-input>
         </el-form-item>
         <!-- 确认密码 -->
         <el-form-item prop='repassword'>
          <el-input type="password" v-model="regForm.repassword" placeholder="请确认密码"></el-input>
         </el-form-item>
         <!-- 按钮 -->
          <el-form-item>
            <el-button @click="handleClick" type="primary" class="reg-btn">注册</el-button>
            <el-link type="info" @click="$router.push('/login')">去登录</el-link>
          </el-form-item>
      </el-form>
      <!-- <p><button @click="handleCk" ref="btn">点击</button> <button @click="handleCk2">点击2</button></p> -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'Reg',
  data() {
    const samePwd = (rule, value, callback) => {
      // rule表示配置的一些规则
      // value表示当前验证的表单域的值
      // callback表示回调函数
      if(value !== this.regForm.password) {
        callback(new Error('俩次输入密码不一致'));
      } else {
        callback()
      }
    }
    return {
      // 注册表单的数据对象
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      // 注册表单的验证规则对象
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的字母数字', trigger: 'blur' }
          // pattern father 模式 基因 样本
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          { validator: samePwd, trigger: 'blur' }  // validator  验证器
        ]
      },
      // info: '显示'
    }
  },
  methods: {
    handleClick() {
      // 表单预验证
      this.$refs.form.validate(async value => {  // validate  证实
        if(value) {
          // console.log('ok');
          const res = await this.$http.post('/api/reg', this.regForm)
          if(res.data.code === 0) {
            this.$router.push('/login')
          } else {
            this.$message.error(res.data.message);
          }
        }
      })
    },
    // handleCk() {
    //   console.log(this.info);
    // },
    // handleCk2() {
    // console.log(this.$refs.btn);
    // this.handleCk()
    // this.$refs.btn.click()
    // },
    // goLogin() {
    //   this.$router.push('/login')
    // }

  }

}
</script>

<style lang="less" scoped>
  .reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    };

    .el-form {
      padding: 0 30px;
      .reg-btn {
        width: 100%;
      }
    }

  }
}
</style>
